import { Injectable } from '@nestjs/common';
import { ChargeOrder } from 'src/entities/ChargeOrder';
import { ChargeOrderModelService } from 'src/model/charge-order-model/charge-order-model.service';
import { ChargeOrderResult } from 'src/type/ChargeOrder.result';
import { isPlatform } from 'src/utils';
import { FindConditions, Like } from 'typeorm';
import ListDto from './dto';

@Injectable()
export class ChargeOrderService {
    constructor(
        private model:ChargeOrderModelService
    ){}

    async list(dto:ListDto, userType) {
        let where:FindConditions<ChargeOrder> = {}

        if(dto.deviceId) {
            where.deviceId = dto.deviceId
        }

        let result:PaginationResult<ChargeOrderResult> = {
            total : 0,
            rows : []
        }
        result.total = await this.model.count(where)
        if(result.total > 0) {
            let fields:(keyof ChargeOrder)[] = ['deviceId', 'portNo', 'orderId', 'orderNo', 'createTime', 'strategyType', 'chargeAmount', 'status', 'consumeAmount', 'stopReason', 'endTime', 'chargeData']
            if(isPlatform(userType)) {
                fields.push('merchantId', 'clientSource', 'userId')
            }

            result.rows = await this.model.pagination(dto.pageNo, dto.pageSize, where, fields, {orderId : 'DESC'})


            if(isPlatform(userType)) {
                let districtIds:number[] = []

                // result.rows.forEach(item => {
                //     if(item.provinceId > 0 && !districtIds.includes(item.provinceId)) {
                //         districtIds.push(item.provinceId)
                //     }
                //     if(item.cityId > 0 && !districtIds.includes(item.cityId)) {
                //         districtIds.push(item.cityId)
                //     }
                //     if(item.districtId > 0 && !districtIds.includes(item.districtId)) {
                //         districtIds.push(item.districtId)
                //     }
                // })

                // if(districtIds.length > 0) {
                //     const districtMap = await this.districtService.getDistrictMapByIds(districtIds)
                //     result.rows.forEach(item => {
                //         item.province = districtMap[item.provinceId]
                //         item.city = districtMap[item.cityId]
                //         item.district = districtMap[item.districtId]
                //     })
                // }
            }

            

            

        }

        return result
    }
}
